{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "ad4d4db0",
   "metadata": {
    "slideshow": {
     "slide_type": "-"
    }
   },
   "source": [
    "# Multiple Input and Multiple Output Channels\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "1b38cfcf",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-08-18T19:36:55.203128Z",
     "iopub.status.busy": "2023-08-18T19:36:55.202806Z",
     "iopub.status.idle": "2023-08-18T19:36:58.529112Z",
     "shell.execute_reply": "2023-08-18T19:36:58.525486Z"
    },
    "origin_pos": 3,
    "tab": [
     "pytorch"
    ]
   },
   "outputs": [],
   "source": [
    "import torch\n",
    "from d2l import torch as d2l"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4dc0fc9a",
   "metadata": {
    "slideshow": {
     "slide_type": "-"
    }
   },
   "source": [
    "Implement cross-correlation operations with multiple input channels"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "c00d6d37",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-08-18T19:36:58.533726Z",
     "iopub.status.busy": "2023-08-18T19:36:58.533243Z",
     "iopub.status.idle": "2023-08-18T19:36:58.538930Z",
     "shell.execute_reply": "2023-08-18T19:36:58.537910Z"
    },
    "origin_pos": 7,
    "tab": [
     "pytorch"
    ]
   },
   "outputs": [],
   "source": [
    "def corr2d_multi_in(X, K):\n",
    "    return sum(d2l.corr2d(x, k) for x, k in zip(X, K))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "116ccd87",
   "metadata": {
    "slideshow": {
     "slide_type": "-"
    }
   },
   "source": [
    "Validate the output"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "4a511891",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-08-18T19:36:58.542978Z",
     "iopub.status.busy": "2023-08-18T19:36:58.542044Z",
     "iopub.status.idle": "2023-08-18T19:36:58.558450Z",
     "shell.execute_reply": "2023-08-18T19:36:58.555512Z"
    },
    "origin_pos": 10,
    "tab": [
     "pytorch"
    ]
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[ 56.,  72.],\n",
       "        [104., 120.]])"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X = torch.tensor([[[0.0, 1.0, 2.0], [3.0, 4.0, 5.0], [6.0, 7.0, 8.0]],\n",
    "               [[1.0, 2.0, 3.0], [4.0, 5.0, 6.0], [7.0, 8.0, 9.0]]])\n",
    "K = torch.tensor([[[0.0, 1.0], [2.0, 3.0]], [[1.0, 2.0], [3.0, 4.0]]])\n",
    "\n",
    "corr2d_multi_in(X, K)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "541f8c5b",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "Calculate the output of multiple channels"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "27621226",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-08-18T19:36:58.571512Z",
     "iopub.status.busy": "2023-08-18T19:36:58.570515Z",
     "iopub.status.idle": "2023-08-18T19:36:58.579033Z",
     "shell.execute_reply": "2023-08-18T19:36:58.578147Z"
    },
    "origin_pos": 14,
    "tab": [
     "pytorch"
    ]
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "torch.Size([3, 2, 2, 2])"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def corr2d_multi_in_out(X, K):\n",
    "    return torch.stack([corr2d_multi_in(X, k) for k in K], 0)\n",
    "\n",
    "K = torch.stack((K, K + 1, K + 2), 0)\n",
    "K.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "4d36175c",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-08-18T19:36:58.583043Z",
     "iopub.status.busy": "2023-08-18T19:36:58.582466Z",
     "iopub.status.idle": "2023-08-18T19:36:58.596203Z",
     "shell.execute_reply": "2023-08-18T19:36:58.593357Z"
    },
    "origin_pos": 16,
    "tab": [
     "pytorch"
    ]
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[[ 56.,  72.],\n",
       "         [104., 120.]],\n",
       "\n",
       "        [[ 76., 100.],\n",
       "         [148., 172.]],\n",
       "\n",
       "        [[ 96., 128.],\n",
       "         [192., 224.]]])"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "corr2d_multi_in_out(X, K)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "57f13519",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "$1 \\times 1$ convolution"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "e0542628",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-08-18T19:36:58.612710Z",
     "iopub.status.busy": "2023-08-18T19:36:58.610896Z",
     "iopub.status.idle": "2023-08-18T19:36:58.627437Z",
     "shell.execute_reply": "2023-08-18T19:36:58.626346Z"
    },
    "origin_pos": 20,
    "tab": [
     "pytorch"
    ]
   },
   "outputs": [],
   "source": [
    "def corr2d_multi_in_out_1x1(X, K):\n",
    "    c_i, h, w = X.shape\n",
    "    c_o = K.shape[0]\n",
    "    X = X.reshape((c_i, h * w))\n",
    "    K = K.reshape((c_o, c_i))\n",
    "    Y = torch.matmul(K, X)\n",
    "    return Y.reshape((c_o, h, w))\n",
    "\n",
    "X = torch.normal(0, 1, (3, 3, 3))\n",
    "K = torch.normal(0, 1, (2, 3, 1, 1))\n",
    "Y1 = corr2d_multi_in_out_1x1(X, K)\n",
    "Y2 = corr2d_multi_in_out(X, K)\n",
    "assert float(torch.abs(Y1 - Y2).sum()) < 1e-6"
   ]
  }
 ],
 "metadata": {
  "celltoolbar": "Slideshow",
  "language_info": {
   "name": "python"
  },
  "required_libs": [],
  "rise": {
   "autolaunch": true,
   "enable_chalkboard": true,
   "overlay": "<div class='my-top-right'><img height=80px src='http://d2l.ai/_static/logo-with-text.png'/></div><div class='my-top-left'></div>",
   "scroll": true
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}